<?php
namespace App\Service;

use Illuminate\Support\Facades\DB;

class BaseService
{
    //基础服务

    protected $database = 'mysql'; //库名称
    protected $db; //表对象

    protected $dbAdmin = 'admin'; //管理员
    protected $dbAction = 'admin_action'; //后台功能
    protected $dbAdLogin = 'admin_login'; //后台登录
    protected $dbAuth = 'admin_auth'; //后台权限
    protected $dbUser = 'user_wx'; //微信用户
    protected $dbUlogin = 'user_login'; //登录日志
    protected $dbUtrace = 'user_trace'; //浏览历史
    protected $dbUlevel = 'user_level'; //会员等级
    protected $dbUaddress = 'user_address'; //收货地址
    protected $dbUtag = 'user_tag'; //用户标签
    protected $dbUcoupon = 'coupon_uid'; //会员劵用户
    protected $dbUcouponTmp = 'coupon_temp'; //会员劵模板
    protected $dbGoods = 'goods'; //商品
    protected $dbGcate = 'goods_cate'; //商品
    protected $dbGimg = 'goods_img'; //商品详情图
    protected $dbGsku = 'goods_sku'; //商品SKU
    protected $dbGkeyword = 'goods_keyword'; //商品关键词
    protected $dbShop = 'shop'; //门店
    protected $dbImg = 'image'; //图片上传
    protected $dbBrand = 'goods_brand'; //品牌
    protected $dbCart = 'cart'; //购物车
    protected $dbOrder = 'order'; //订单
    protected $dbOsku = 'order_sku'; //订单SKU信息
    protected $dbAddress = 'user_address'; //收货地址
    protected $dbPay = 'pay'; //支付
    protected $dbTixian = 'user_withdraw'; //提现
    protected $dbPayNotice = 'pay_notice'; //支付通知
    protected $dbPayRefund = 'pay_refund'; //退款
    protected $dbPayNotice2 = 'pay_notice2'; //退款通知
    protected $dbDoc = 'doc'; //文档
    protected $dbDocPam = 'doc_pam'; //文档字段
    protected $dbDocCount = 'doc_count'; //文档统计

    //二期发现
    protected $dbFind = 'cms_post'; //话题
    protected $dbFindCate = 'cms_category'; //分类
    protected $dbFindImg = 'cms_post_img'; //图片
    //二期素材
    protected $dbSucai = 'sucai'; //素材
    protected $dbScFile = 'sucai_file'; //素材文件
    protected $dbCheap = 'cheap_limit'; //限时折扣
    /**
     * 渠道端
     */
    protected $dbQdShop = 'distributor_wx'; //渠道商
    protected $dbQdGcate = 'distributor_goods_cate'; //商品分类
    protected $dbQdGoods = 'distributor_goods'; //商品
    protected $dbQdGsku = 'distributor_goods_sku'; //商品Sku
    protected $dbQdGimg = 'distributor_goods_img'; //商品图片
    protected $dbQdGword = 'distributor_goods_keyword'; //商品搜索词
    protected $dbQdCart = 'distributor_cart'; //购物车
    protected $dbQdOrder = 'distributor_order'; //订单
    protected $dbQdOsku = 'distributor_order_sku'; //订单SKU
    protected $dbQdAddress = 'distributor_address'; //收货地址
    protected $dbQdSucai = 'distributor_material'; //素材
    protected $dbQdScimg = 'distributor_material_img'; //素材图片
    protected $dbQdIncome = 'distributor_income'; //收益
    protected $dbQdCard = 'card'; //礼品卡
    protected $dbQdCcate = 'card_category'; //礼品卡分类
    protected $dbQdCimg = 'card_img'; //礼品卡图片
    protected $dbQdCgoods = 'card_goods'; //礼品卡商品
    protected $dbQdCuser = 'card_user'; //礼品卡商品
    protected $dbQdPay = 'distributor_pay'; //充值记录
    protected $dbQdTixian = 'distributor_withdraw'; //提现记录
    protected $dbQdBanner = 'banner';//轮播图
    protected $dbQdConfig = 'admin_setting';//配置
    protected $dbEqGoods = 'points_goods';//礼品卡商城
    protected $dbEqGcate = 'points_goods_cate';//礼品卡分类
    protected $dbEqGimg = 'points_goods_img';//礼品卡图片
    protected $dbEqGsku = 'points_goods_sku';//礼品卡SKU
    protected $dbEqJifen = 'user_points_log';//积分
    protected $dbEqJfOrder = 'points_order';//积分兑换的
    protected $dbEqCost = 'distributor_cost';//费用
    /**
     * 渠道外包
     */
    protected $dbUserPointLog = 'user_points_log'; //积分变动日志
    protected $dbDistributorUser = 'distributor_wx'; //渠道商用户
    protected $dbDistributorTrace = 'distributor_trace'; //浏览历史
    protected $dbDistributorUaddress = 'distributor_address'; //渠道商收货地址
    protected $dbPointsGoods = 'points_goods'; //积分商品
    protected $dbPointsGcate = 'points_goods_cate'; //积分商品分类
    protected $dbPointsGimg = 'points_goods_img'; //积分商品详情图
    protected $dbPointsGsku = 'points_goods_sku'; //积分商品SKU
    protected $dbPointsOrder = 'points_order'; //积分商品订单
    protected $dbPointsOsku = 'points_order_sku'; //积分订单商品SKU
    protected $dbCard = 'card';
    protected $dbCardCate = 'card_category';
    protected $dbCardGoods = 'card_goods';
    protected $dbCardImg = 'card_img';
    protected $dbCardUser = 'card_user';
    protected $dbDistributorGoods = 'distributor_goods'; //渠道商商品
    protected $dbDistributorGcate = 'distributor_goods_cate'; //渠道商商品
    protected $dbDistributorGimg = 'distributor_goods_img'; //渠道商商品详情图
    protected $dbDistributorGsku = 'distributor_goods_sku'; //渠道商商品SKU
    protected $dbDistributorGkeyword = 'distributor_goods_keyword'; //渠道商商品关键词
    protected $dbDistributorCart = 'distributor_cart'; //渠道商购物车
    protected $dbDistributorOrder = 'distributor_order'; //渠道商订单
    protected $dbDistributorOsku = 'distributor_order_sku'; //渠道商订单SKU信息
    protected $dbDistributorPay = 'distributor_pay'; //渠道商支付
    protected $dbDistributorPayNotice = 'distributor_pay_notice'; //渠道商支付通知
    protected $dbDistributorPayRefund = 'distributor_pay_refund'; //渠道商退款
    protected $dbDistributorPayNotice2 = 'distributor_pay_notice2'; //渠道商退款通知
    protected $dbDistributorMaterial = 'distributor_material';//素材
    protected $dbDistributorMaterialImg = 'distributor_material_img';//素材图片
    protected $dbUserIncome = 'user_income';//收益
    protected $dbUserWithdraw = 'user_withdraw';//提现记录
    protected $dbUserWithdrawInfo = 'user_withdraw_info';//提现用户信息
    protected $dbDistributorIncome = 'distributor_income';//收益
    protected $dbDistributorWithdraw = 'distributor_withdraw';//提现记录
    protected $dbDistributorWithdrawInfo = 'distributor_withdraw_info';//提现用户信息
    protected $dbCmsPost = 'cms_post';//帖子
    protected $dbCmsCategory = 'cms_category';//帖子分类
    protected $dbCmsImg = 'cms_post_img';//帖子图片

    //订单状态
    protected $orderStatus1s = [
        1 => '未付款', '未发货', '未收货', '已完成',
    ];
    //订单状态
    protected $orderStatus2s = [
        1 => '未付款', '门店自提', '自提成功',
    ];
    //快递选项
    protected $orderDeliverys = [
        1 => '第三方快递', '到店取货',
    ];
    //是否取消
    protected $orderCancels = [
        1 => '正常', '审核中', '售后',
    ];
    //是否退货
    protected $orderTuihuos = [
        1 => '正常', '审核中', '售后',
    ];
    //支付方式
    protected $orderPayWays = [
        1 => '微信支付', '余额支付',
    ];
    //地址类型
    protected $addressTypes = [
        1 => '家', '公司', '父母家',
    ];
    //支付状态
    protected $payStatuss = [
        '未发起', '发起支付', '通知成功',
    ];

    //提现状态
    protected $withdrawStatus = [
        1 => '审核中', '已提现成功', '提现失败',
    ];

    //返回参数
    public function toback($code = 0, $msg = '', $data = [], $total = 0)
    {
        $msg = $msg ? $msg : 'success';
        return array('code' => $code, 'msg' => $msg, 'data' => $data, 'total' => $total);
    }

    /**
     * 查询列表
     * @param string $db 表名
     * @param array $w1s【参数名=>值】
     * @param array $w2s【参数名，操作符，值】
     * @param array $sels 字段数组（返回字段=>表字段）
     */
    public function dbQueryList($db, $w1s = [], $w2s = [], $sels = [])
    {
//        return DB::connection($this->database)->table($db)
//            //->select('id', 'title')
//            ->where('del', 1)
//            ->get();
        $db = $this->dbConn($db);
        if (!empty($w1s)) {
            foreach ($w1s as $k => $v) {
                $db = $db->where($k, $v);
            }
        }
        if (!empty($w2s)) {
            foreach ($w2s as $v) {
                $db = $db->where($v['key'], $v['oper'], $v['val']);
            }
        }
        $lists = $db->get();
        return $this->listSelect($lists, $sels);
    }

    //列表指定字段转换
    public function listSelect($lists, $fields)
    {
        $arys = [];
        if (!count($lists)) {
            return $arys;
        }
        foreach ($lists as $v) {
            $v = (array) $v;
            $ary2s = [];
            foreach ($fields as $kk => $vv) {
                if (isset($v[$vv])) {
                    $ary2s[$kk] = $v[$vv];
                }
            }
            if (!empty($ary2s)) {
                $arys[] = $ary2s;
            }
        }
        return $arys;
    }

    /**
     * 查询一个
     * @param string $db 表名
     * @param array $iswheres【参数名=>值】
     * @param array $opers【0参数名，1操作符，2值】
     * @param array $sels 字段数组（返回字段=>表字段）
     */
    public function dbQueryOne($db, $w1s = [], $w2s = [], $sels = [])
    {
        $db = $this->dbConn($db);
        if (!empty($w1s)) {
            foreach ($w1s as $k => $v) {
                $db = $db->where($k, $v);
            }
        }
        if (!empty($w2s)) {
            foreach ($w2s as $v) {
                $db = $db->where($v[0], $v[1], $v[2]);
            }
        }
        $one = (array) $db->first();
        return $this->oneSelect($one, $sels);
    }

    //列表指定字段转换
    public function oneSelect($one, $fields)
    {
        if (empty($fields)) {
            return $one;
        }
        $arys = [];
        if (empty($one)) {
            return $arys;
        }
        foreach ($fields as $kk => $vv) {
            if (isset($one[$vv])) {
                $arys[$kk] = $one[$vv];
            }
        }
        return $arys;
    }

    /**
     * 插入一条
     * @param string $db 表名
     * @param array $pas 数据
     */
    public function dbInsertOne($db, $pas)
    {
        return $this->dbConn($db)->insertGetId($pas);
    }

    /**
     * 修改
     * @param string $db 表名
     * @param array $w1s【$key，$val】
     * @param array $pas 数据【$key，$val】
     */
    public function dbModify($db, $w1s, $pas)
    {
        $db = $this->dbConn($db);
        if (count($w1s)) {
            foreach ($w1s as $k => $v) {
                $db = $db->where($k, $v);
            }
        }
        return $db->update($pas);
    }

    /**
     * 连接数据库
     * @param string $db 表名
     * @param array $ws 查询条件【key=>val】
     */
    public function dbConn($db, $ws=[])
    {
        $db = DB::connection($this->database)->table($db);
        if (!empty($ws)) {
            foreach ($ws as $k => $v) {
                $db = $db->where($k, $v);
            }
        }
        return $db;
    }

    /**
     * 连接二期数据库
     */
    public function db2Conn($db, $ws=[])
    {
        $db = DB::connection('mysql2')->table($db);
        if (!empty($ws)) {
            foreach ($ws as $k => $v) {
                $db = $db->where($k, $v);
            }
        }
        return $db;
    }

    /**
     * 连接二期数据库，外包使用
     */
    public function dbwbConn($db, $ws=[])
    {
        $db = DB::connection('mysql2')->table($db);
        if (!empty($ws)) {
            foreach ($ws as $k => $v) {
                $db = $db->where($k, $v);
            }
        }
        return $db;
    }
}